package com.hymjweb.demo.framework.menu.controller;

import com.hymjweb.demo.framework.menu.bean.MenuBean;
import com.hymjweb.demo.framework.menu.bean.MenuPojo;
import com.hymjweb.demo.framework.menu.service.MenuService;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.framework.menu
 * @Author: luoge
 * @CreateTime: 2020-10-06 09:27
 * @Description:
 */

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

    private static List<MenuBean> menuBeanList;


    @Autowired
    private MenuService menuService;

    @RequestMapping("/showMenuPage")
    public String showMenuPage(){

        // return "/pages/menu_manage/menuItemList";
        return "/pages/menu_manage/menuItemTreeList";
    }

    @RequestMapping("/loadMenuTree")
    @ResponseBody
    public Object loadMenuTree(){
        Object menuPojoList = menuService.getNoPageList(null);
        return menuPojoList;
    }

    @RequestMapping("/getMenuNoPageList")
    @ResponseBody
    public Object getMenuNoPageList(@RequestParam(required = true) Map param) throws Exception {
        if(param == null){
            throw new Exception("菜单加载参数错误!");
        }
        Object menuPojoList = menuService.getNoPageList(param);
        return menuPojoList;
    }

    /**
     * 根据Pid获取菜单项目
     * @return
     */
    @RequestMapping("/getMenuList")
    @ResponseBody
    public Object getMenuList(@RequestParam(required = true) Map param) throws Exception {
        if(param == null){
            throw new Exception("菜单加载参数错误!");
        }
        Object menuPojoList = menuService.getPageList(param);
        return menuPojoList;
    }


    @RequestMapping("/getMenuAndChildrenList")
    @ResponseBody
    public List<MenuBean> getMenuAndChildrenList() {
        menuBeanList = menuService.getMenuAndChildrenList();
        return menuBeanList;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(@RequestBody(required = true) MenuBean menuBean,String opType) throws Exception {
        if(menuBean == null){
            throw new Exception("保存菜单参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();

        int affectRows = SysConstant.INT_ZERO;

        if(opType.equals(SysConstant.OPER_ADD)){
            // 这是添加
            baseResultMap.setOk(true);
            baseResultMap.setMessage("添加菜单成功!");
            affectRows = menuService.addMenu(menuBean);

            if(affectRows<=SysConstant.INT_ZERO){
                baseResultMap.setOk(false);
                baseResultMap.setMessage("添加菜单失败，请重试!");
            }
            else{
                // 刷新菜单缓存
                refreshMenu();
            }
        }
        else{
            // 这是更新
            baseResultMap.setOk(true);
            baseResultMap.setMessage("更新菜单成功!");

            affectRows = menuService.updateMenu(menuBean);

            if(affectRows<=SysConstant.INT_ZERO){
                baseResultMap.setOk(false);
                baseResultMap.setMessage("更新菜单失败，请重试!");
            }
            else{
                // 刷新菜单缓存
                refreshMenu();
            }
        }

        return baseResultMap;
    }

    /**
     * 刷新菜单
     */
    private void refreshMenu(){
        // 清空菜单列表
        menuBeanList.clear();
        // 重新加载一次
        menuBeanList = menuService.getMenuAndChildrenList();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) MenuPojo menuPojo) throws Exception {
        if(menuPojo == null){
            throw new Exception("删除菜单参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("删除菜单成功!");

        int affectRows = menuService.delete(menuPojo);
        if(affectRows<= SysConstant.INT_ZERO){
            baseResultMap.setOk(false);
            baseResultMap.setMessage("删除菜单失败!");
        }
        else{
            // 刷新菜单缓存
            refreshMenu();
        }
        return baseResultMap;
    }


    @RequestMapping("/checkMenuId")
    @ResponseBody
    public Object checkMenuId(String menuId) throws Exception {
        if(menuId == null){
            throw new Exception("检测菜单ID参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("检测菜单ID成功!");

        MenuBean menuBean = menuService.getMenuById(menuId);
        if(menuBean!=null){
            baseResultMap.setOk(false);
            baseResultMap.setMessage("根据ID检测到已经有该ID的菜单项存在，不能重复添加相同ID的菜单项!");
        }
        return baseResultMap;
    }


}
